package srcInterviewPrep;

public final class StressJobScheduler
{
    public static int GetOptimalScheduleValue(int[] lJobs, int[] hJobs)
    {
        // null check, empty check, equal length check for both arrays

        int opt1 = 0;
        int opt2 = Math.max(lJobs[0], hJobs[0]);
        int n = lJobs.length;

        for (int i = 2; i <= n; ++i)
        {
            int opt = Math.max(lJobs[i-1] + opt2, hJobs[i-1] + opt1);
            opt1 = opt2;
            opt2 = opt;
        }

        return opt2;
    }

    public static char[] GetOptimalScheduleValueV2(int[] lJobs, int[] hJobs)
    {
        // null check, empty check, equal length check for both arrays

        int opt1 = 0;
        int opt2 = Math.max(lJobs[0], hJobs[0]);
        int n = lJobs.length;

        char[] s = new char[n+1];
        s[0] = 'i';
        s[1] = lJobs[0] > hJobs[0] ? 'l' : 'h';

        for (int i = 2; i <= n; ++i)
        {
            int opt = Math.max(lJobs[i-1] + opt2, hJobs[i-1] + opt1);
            s[i] = lJobs[i-1] + opt2 > hJobs[i-1] + opt1 ? 'l' : 'h';

            opt1 = opt2;
            opt2 = opt;
        }

        System.out.println(opt2);
        return GetOptimalSchedule(n, s);
    }

    private static char[] GetOptimalSchedule(int n, char[] s)
    {
        char[] schedule = new char[n];
        schedule[0] = s[1];

        int i = n;

        while (i >= 2)
        {
            if (s[i] == 'l')
            {
                schedule[i-1] = 'l';
                --i;
            }
            else
            {
                schedule[i-1] = 'h';
                schedule[i-2] = 'n';
                i -= 2;
            }
        }

        return schedule;
    }
}
